package com.example.star.controller;

import com.example.star.entity.Favorite;
import com.example.star.entity.Feedback;
import com.example.star.entity.Follow;
import com.example.star.entity.User;
import com.example.star.service.FeedbackService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    @PostMapping("/feedback")
    @ResponseBody
    public void createFeedback(@RequestBody Feedback feedback) {
        feedbackService.createFeedback(feedback);
    }

    @GetMapping("/feedback")
    @ResponseBody
    public List<Feedback> getAllFeedbacks() {
        return feedbackService.getAllFeedbacks();
    }

    @GetMapping("/feedback/admin/{adminName}")
    @ResponseBody
    public List<Feedback> getFeedbacksByAdminId(@PathVariable String adminName) {
        return feedbackService.getFeedbacksByAdminName(adminName);
    }

    @GetMapping("/feedback-list")
    public String showFeedbackList(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "8") int size,
                                   Model model,
                                   HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            // 用户未登录，重定向到登录页面

        }
        User user = (User) userObj;
        List<Feedback> feedback;
        int totalStars;
        if ("ADMIN".equals(user.getRole())) {
            // 管理员角色，获取所有收藏信息
            feedback = feedbackService.getFeedbackByPage(page, size);
            totalStars = feedbackService.getTotalFeedback();
        } else {
            // 普通用户角色，获取该用户的收藏信息
            feedback = feedbackService.getFeedbackByUserPage(user.getUsername(), page, size);
            totalStars = feedbackService.getTotalFeedbackByUser(user.getUsername());
        }
        int totalPages = (int) Math.ceil((double) totalStars / size);

        model.addAttribute("feedback", feedback);
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", totalPages);

        return "feedback-list";
    }

    @PostMapping("/feedback/{id}")
    public String deleteFeedBack(@PathVariable int id, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        // 从 HttpSession 中获取用户信息
        HttpSession session = request.getSession();
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            redirectAttributes.addFlashAttribute("error", "你还未登录，请先登录");
            return "redirect:/login"; // 重定向到登录页面
        }

        User user = (User) userObj;
        // 假设管理员角色名称为 "ADMIN"，根据实际情况修改
        if ("ADMIN".equals(user.getRole())) {
            try {
                feedbackService.deleteFeedbackById(id);
                redirectAttributes.addFlashAttribute("message", "反馈信息删除成功");
            } catch (Exception e) {
                redirectAttributes.addFlashAttribute("error", "删除反馈信息时出现错误：" + e.getMessage());
            }
        } else {
            redirectAttributes.addFlashAttribute("error", "你没有权限删除反馈信息");
        }
        return "redirect:/feedback-list";
    }

}


